package com.example.mylibrary.greendao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.example.mylibrary.data.Sort;

import com.example.mylibrary.data.StudentInfo;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "STUDENT_INFO".
*/
public class StudentInfoDao extends AbstractDao<StudentInfo, Long> {

    public static final String TABLENAME = "STUDENT_INFO";

    /**
     * Properties of entity StudentInfo.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property StudentID = new Property(0, Long.class, "studentID", true, "_id");
        public final static Property StudenCode = new Property(1, String.class, "studenCode", false, "STUDEN_CODE");
        public final static Property StudentName = new Property(2, String.class, "studentName", false, "STUDENT_NAME");
        public final static Property IdCard = new Property(3, String.class, "idCard", false, "ID_CARD");
        public final static Property IcCardNo = new Property(4, String.class, "icCardNo", false, "IC_CARD_NO");
        public final static Property SortID = new Property(5, long.class, "sortID", false, "SORT_ID");
        public final static Property Sex = new Property(6, int.class, "sex", false, "SEX");
        public final static Property Nation = new Property(7, String.class, "nation", false, "NATION");
        public final static Property Birthday = new Property(8, String.class, "birthday", false, "BIRTHDAY");
        public final static Property Address = new Property(9, String.class, "address", false, "ADDRESS");
        public final static Property Authority = new Property(10, String.class, "authority", false, "AUTHORITY");
        public final static Property School = new Property(11, String.class, "school", false, "SCHOOL");
        public final static Property GradeName = new Property(12, String.class, "gradeName", false, "GRADE_NAME");
        public final static Property ClassName = new Property(13, String.class, "className", false, "CLASS_NAME");
        public final static Property Finger = new Property(14, byte[].class, "finger", false, "FINGER");
        public final static Property FacePhoto = new Property(15, byte[].class, "facePhoto", false, "FACE_PHOTO");
        public final static Property FacePath = new Property(16, String.class, "facePath", false, "FACE_PATH");
        public final static Property Device = new Property(17, String.class, "device", false, "DEVICE");
        public final static Property UploadState = new Property(18, int.class, "uploadState", false, "UPLOAD_STATE");
        public final static Property UploadTime = new Property(19, String.class, "uploadTime", false, "UPLOAD_TIME");
        public final static Property RollCallState = new Property(20, int.class, "rollCallState", false, "ROLL_CALL_STATE");
        public final static Property FinalRollCalltime = new Property(21, String.class, "finalRollCalltime", false, "FINAL_ROLL_CALLTIME");
    }

    private DaoSession daoSession;


    public StudentInfoDao(DaoConfig config) {
        super(config);
    }
    
    public StudentInfoDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"STUDENT_INFO\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: studentID
                "\"STUDEN_CODE\" TEXT UNIQUE ," + // 1: studenCode
                "\"STUDENT_NAME\" TEXT," + // 2: studentName
                "\"ID_CARD\" TEXT," + // 3: idCard
                "\"IC_CARD_NO\" TEXT," + // 4: icCardNo
                "\"SORT_ID\" INTEGER NOT NULL ," + // 5: sortID
                "\"SEX\" INTEGER NOT NULL ," + // 6: sex
                "\"NATION\" TEXT," + // 7: nation
                "\"BIRTHDAY\" TEXT," + // 8: birthday
                "\"ADDRESS\" TEXT," + // 9: address
                "\"AUTHORITY\" TEXT," + // 10: authority
                "\"SCHOOL\" TEXT," + // 11: school
                "\"GRADE_NAME\" TEXT," + // 12: gradeName
                "\"CLASS_NAME\" TEXT," + // 13: className
                "\"FINGER\" BLOB," + // 14: finger
                "\"FACE_PHOTO\" BLOB," + // 15: facePhoto
                "\"FACE_PATH\" TEXT," + // 16: facePath
                "\"DEVICE\" TEXT," + // 17: device
                "\"UPLOAD_STATE\" INTEGER NOT NULL ," + // 18: uploadState
                "\"UPLOAD_TIME\" TEXT," + // 19: uploadTime
                "\"ROLL_CALL_STATE\" INTEGER NOT NULL ," + // 20: rollCallState
                "\"FINAL_ROLL_CALLTIME\" TEXT);"); // 21: finalRollCalltime
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"STUDENT_INFO\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, StudentInfo entity) {
        stmt.clearBindings();
 
        Long studentID = entity.getStudentID();
        if (studentID != null) {
            stmt.bindLong(1, studentID);
        }
 
        String studenCode = entity.getStudenCode();
        if (studenCode != null) {
            stmt.bindString(2, studenCode);
        }
 
        String studentName = entity.getStudentName();
        if (studentName != null) {
            stmt.bindString(3, studentName);
        }
 
        String idCard = entity.getIdCard();
        if (idCard != null) {
            stmt.bindString(4, idCard);
        }
 
        String icCardNo = entity.getIcCardNo();
        if (icCardNo != null) {
            stmt.bindString(5, icCardNo);
        }
        stmt.bindLong(6, entity.getSortID());
        stmt.bindLong(7, entity.getSex());
 
        String nation = entity.getNation();
        if (nation != null) {
            stmt.bindString(8, nation);
        }
 
        String birthday = entity.getBirthday();
        if (birthday != null) {
            stmt.bindString(9, birthday);
        }
 
        String address = entity.getAddress();
        if (address != null) {
            stmt.bindString(10, address);
        }
 
        String authority = entity.getAuthority();
        if (authority != null) {
            stmt.bindString(11, authority);
        }
 
        String school = entity.getSchool();
        if (school != null) {
            stmt.bindString(12, school);
        }
 
        String gradeName = entity.getGradeName();
        if (gradeName != null) {
            stmt.bindString(13, gradeName);
        }
 
        String className = entity.getClassName();
        if (className != null) {
            stmt.bindString(14, className);
        }
 
        byte[] finger = entity.getFinger();
        if (finger != null) {
            stmt.bindBlob(15, finger);
        }
 
        byte[] facePhoto = entity.getFacePhoto();
        if (facePhoto != null) {
            stmt.bindBlob(16, facePhoto);
        }
 
        String facePath = entity.getFacePath();
        if (facePath != null) {
            stmt.bindString(17, facePath);
        }
 
        String device = entity.getDevice();
        if (device != null) {
            stmt.bindString(18, device);
        }
        stmt.bindLong(19, entity.getUploadState());
 
        String uploadTime = entity.getUploadTime();
        if (uploadTime != null) {
            stmt.bindString(20, uploadTime);
        }
        stmt.bindLong(21, entity.getRollCallState());
 
        String finalRollCalltime = entity.getFinalRollCalltime();
        if (finalRollCalltime != null) {
            stmt.bindString(22, finalRollCalltime);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, StudentInfo entity) {
        stmt.clearBindings();
 
        Long studentID = entity.getStudentID();
        if (studentID != null) {
            stmt.bindLong(1, studentID);
        }
 
        String studenCode = entity.getStudenCode();
        if (studenCode != null) {
            stmt.bindString(2, studenCode);
        }
 
        String studentName = entity.getStudentName();
        if (studentName != null) {
            stmt.bindString(3, studentName);
        }
 
        String idCard = entity.getIdCard();
        if (idCard != null) {
            stmt.bindString(4, idCard);
        }
 
        String icCardNo = entity.getIcCardNo();
        if (icCardNo != null) {
            stmt.bindString(5, icCardNo);
        }
        stmt.bindLong(6, entity.getSortID());
        stmt.bindLong(7, entity.getSex());
 
        String nation = entity.getNation();
        if (nation != null) {
            stmt.bindString(8, nation);
        }
 
        String birthday = entity.getBirthday();
        if (birthday != null) {
            stmt.bindString(9, birthday);
        }
 
        String address = entity.getAddress();
        if (address != null) {
            stmt.bindString(10, address);
        }
 
        String authority = entity.getAuthority();
        if (authority != null) {
            stmt.bindString(11, authority);
        }
 
        String school = entity.getSchool();
        if (school != null) {
            stmt.bindString(12, school);
        }
 
        String gradeName = entity.getGradeName();
        if (gradeName != null) {
            stmt.bindString(13, gradeName);
        }
 
        String className = entity.getClassName();
        if (className != null) {
            stmt.bindString(14, className);
        }
 
        byte[] finger = entity.getFinger();
        if (finger != null) {
            stmt.bindBlob(15, finger);
        }
 
        byte[] facePhoto = entity.getFacePhoto();
        if (facePhoto != null) {
            stmt.bindBlob(16, facePhoto);
        }
 
        String facePath = entity.getFacePath();
        if (facePath != null) {
            stmt.bindString(17, facePath);
        }
 
        String device = entity.getDevice();
        if (device != null) {
            stmt.bindString(18, device);
        }
        stmt.bindLong(19, entity.getUploadState());
 
        String uploadTime = entity.getUploadTime();
        if (uploadTime != null) {
            stmt.bindString(20, uploadTime);
        }
        stmt.bindLong(21, entity.getRollCallState());
 
        String finalRollCalltime = entity.getFinalRollCalltime();
        if (finalRollCalltime != null) {
            stmt.bindString(22, finalRollCalltime);
        }
    }

    @Override
    protected final void attachEntity(StudentInfo entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public StudentInfo readEntity(Cursor cursor, int offset) {
        StudentInfo entity = new StudentInfo( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // studentID
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // studenCode
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // studentName
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // idCard
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // icCardNo
            cursor.getLong(offset + 5), // sortID
            cursor.getInt(offset + 6), // sex
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // nation
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // birthday
            cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9), // address
            cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10), // authority
            cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11), // school
            cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12), // gradeName
            cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13), // className
            cursor.isNull(offset + 14) ? null : cursor.getBlob(offset + 14), // finger
            cursor.isNull(offset + 15) ? null : cursor.getBlob(offset + 15), // facePhoto
            cursor.isNull(offset + 16) ? null : cursor.getString(offset + 16), // facePath
            cursor.isNull(offset + 17) ? null : cursor.getString(offset + 17), // device
            cursor.getInt(offset + 18), // uploadState
            cursor.isNull(offset + 19) ? null : cursor.getString(offset + 19), // uploadTime
            cursor.getInt(offset + 20), // rollCallState
            cursor.isNull(offset + 21) ? null : cursor.getString(offset + 21) // finalRollCalltime
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, StudentInfo entity, int offset) {
        entity.setStudentID(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setStudenCode(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setStudentName(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setIdCard(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setIcCardNo(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setSortID(cursor.getLong(offset + 5));
        entity.setSex(cursor.getInt(offset + 6));
        entity.setNation(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setBirthday(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setAddress(cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
        entity.setAuthority(cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10));
        entity.setSchool(cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11));
        entity.setGradeName(cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12));
        entity.setClassName(cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13));
        entity.setFinger(cursor.isNull(offset + 14) ? null : cursor.getBlob(offset + 14));
        entity.setFacePhoto(cursor.isNull(offset + 15) ? null : cursor.getBlob(offset + 15));
        entity.setFacePath(cursor.isNull(offset + 16) ? null : cursor.getString(offset + 16));
        entity.setDevice(cursor.isNull(offset + 17) ? null : cursor.getString(offset + 17));
        entity.setUploadState(cursor.getInt(offset + 18));
        entity.setUploadTime(cursor.isNull(offset + 19) ? null : cursor.getString(offset + 19));
        entity.setRollCallState(cursor.getInt(offset + 20));
        entity.setFinalRollCalltime(cursor.isNull(offset + 21) ? null : cursor.getString(offset + 21));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(StudentInfo entity, long rowId) {
        entity.setStudentID(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(StudentInfo entity) {
        if(entity != null) {
            return entity.getStudentID();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(StudentInfo entity) {
        return entity.getStudentID() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getSortDao().getAllColumns());
            builder.append(" FROM STUDENT_INFO T");
            builder.append(" LEFT JOIN SORT T0 ON T.\"SORT_ID\"=T0.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected StudentInfo loadCurrentDeep(Cursor cursor, boolean lock) {
        StudentInfo entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Sort sortid = loadCurrentOther(daoSession.getSortDao(), cursor, offset);
         if(sortid != null) {
            entity.setSortid(sortid);
        }

        return entity;    
    }

    public StudentInfo loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<StudentInfo> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<StudentInfo> list = new ArrayList<StudentInfo>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<StudentInfo> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<StudentInfo> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
